﻿using System.Collections.Generic;
using System.Drawing;
using com.fukefu.source;
using com.fukefu.sql;

namespace com.fukefu.info.b2b.byhand
{
    public class YouKu : Information
    {
        List<string> tag = new List<string>();
        List<string> vidio = new List<string>();
        SqlServerOperate sql = new SqlServerOperate();
        //刷新验证码
        public override Image GetFlushCodeImage()
        {
            return source.GetImage("http://u.youku.com/verify/", "http://www.youku.com/");
        }
        //登陆
        public override bool Login(string name, string pass, string vcode)
        {
            ParametersData pd = new ParametersData();
            pd.Add("__ap", "{\"username\": \"" + name + "\", \"password\": \"" + pass + "\", \"forever\": \"1\"}");

            if (source.Post("http://www.youku.com/QVideo/~ajax/logon?__rt=1&__ro=", pd.Read(), "http://www.youku.com/").Equals("true"))
            {
                LoadInformation();
                return true;
            }
            return false;
        }
        //发布一篇文章
        public override InformationResult PostArticle(string key, string content, string captcha)
        {
            InformationResult info = new InformationResult();
            /*string html = "";
             //__ap	{"tags": "fdf fdfd"}
             string tags = CreateTag();
             string title = title.Word;
             if (source.Post("http://u.youku.com/QTag/~ajax/verifyTags?__rt=1&__ro=", "__ap=" + MyFunction.EncodeByUtf8("{\"tags\": \"" + tags + "\"}"), "http://u.youku.com/my_video/type_playlist.html").Equals("1"))
             {
                 html = source.Post("http://u.youku.com/QMy/~ajax/createFolder?__rt=1&__ro=", "__ap=" + MyFunction.EncodeByUtf8("{\"name\": \"" + title + "\", \"desc\": \"" + temp.GetDesc(title.Word) + "\", \"tags\": \"" + tags + "\", \"cateid\": \"134\", \"vids\": \"\", \"encodevids\": \"" + CreateVidio() + "\", \"verify_code\": \"" + captcha + "\"}").Replace("+", "%20"), "http://u.youku.com/my_video/type_playlist.html");
                 if (html.StartsWith("\""))
                 {
                     return "http://www.youku.com/playlist_show/id_" + Regex.Replace(html, "\"", "") + ".html";
                 }
             }*/
            return info;
        }
        //初始化数据
        private void LoadInformation()
        {
            //加载标签
            /*SqlDataReader re = sql.Query("select * from [tag]");
            while (re.Read())
            {
                tag.Add(re["cont"].ToString());
            }
            re.Close();

            //加载视频
            [re = sql.Query("select * from [视频列表] where wid='" + + "'");]
            while (re.Read())
            {
                vidio.Add(re["cont"].ToString());
            }
            re.Close();*/
        }
        //生成随机tag
        private string CreateTag()
        {
            int[] re = MyFunction.GetDifferentRandomNum(tag.Count, 2 + random.Next(4));
            string result = "";
            foreach (int i in re)
            {
                result += (result.Equals("") ? "" : " ") + tag[i];
            }
            return result;
        }
        //生成随机视频
        private string CreateVidio()
        {
            int[] re = MyFunction.GetDifferentRandomNum(vidio.Count, 3 + random.Next(4));
            string result = "";
            foreach (int i in re)
            {
                result += (result.Equals("") ? "" : ",") + vidio[i];
            }
            return result;
        }
    }
}

